package com.gali.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: gali
 * @Date: 2022-08-18 21:27
 * @Description:
 */
public class CompletableFutureCombine {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "come in1");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName() + "come in2 ");
            return 20;
        }),(x,y)->{
            System.out.println("计算中....."+(x+y));
           return x+y;
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName() + "come in3 ");
            return 30;
        }),(x,y)->{
            System.out.println("计算中....."+(x+y));
            return x+y;
        });
        System.out.println(Thread.currentThread().getName()+"主线程");
        System.out.println(completableFuture.get());
    }
}
